#include "widget.h"
#include "ui_widget.h"
#include<QMessageBox>
#include"public.h"
#include<QFile>
#include <QFileInfo>
#include <QDataStream>
#include<string.h>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    mp_clientSocket = new QTcpSocket();
    QString ip = "192.168.125.128";
    int port = 9000;
    mp_clientSocket->connectToHost(ip, port);
    if(!mp_clientSocket->waitForConnected(30000))
    {
        QMessageBox::information(this, "QT网络通信", "连接服务端失败！");
        return;
    }
    connect(mp_clientSocket, SIGNAL(readyRead), this, SLOT(ClientRevcData));
}

Widget::~Widget()
{
    delete ui;
}
//C:\Users\YoyonK\Desktop\a.jpg

void Widget::ClientRevcData(){
    char recvMsg[1024] = {0};
    int recvRe = mp_clientSocket->read(recvMsg, 1024);
    if(recvRe == -1)
    {
        QMessageBox::information(this, "QT网络通信", "接收服务端数据失败！");
        return;
    }
    QString showQstr = recvMsg;
    ui->m_recvTextEdit_2->setText(showQstr);
}

void Widget::on_pushButton_clicked()
{



    QFile file("a.jpg");
    file.open(QIODevice::ReadOnly);
    QByteArray array;
    char arry[10000];
    array = file.readAll();
    memcpy(arry,array.data(),array.length());
file.close();

shopinf shinf;
memcpy(shinf.cName,"wode",4);
memcpy(shinf.cAddr,"wode",4);
memcpy(shinf.cState,"wode",4);
memcpy(shinf.Shopphoto.photo,array.data(),array.length());
shinf.Shopphoto.upLen = array.length();

Nethead Net;
Net.iBl = 0;
Net.bonil = 1;
Net.iUnitID = UNIT_SHOP_INF;
memcpy(Net.lParam,(char*)&shinf,sizeof(shopinf));
Net.ilenth =sizeof(shopinf) ;
int sendre = mp_clientSocket->write((char*)&Net, sizeof(Net));


//    QString sendMsg = "aaa";
//    char sendMsgChar[1024] = {0};
//    strcpy_s(sendMsgChar, sendMsg.toStdString().c_str());
//    int sendRe = mp_clientSocket->write(sendMsgChar, strlen(sendMsgChar));
    if(sendre == -1)
    {
        QMessageBox::information(this, "QT网络通信", "向服务端发送数据失败！");
        return;
    }

}

void Widget::on_pushButton_2_clicked()
{
    QFile file;
    file.setFileName("C:/Users/YoyonK/Desktop/a.jpg");
   if(file.open(QIODevice::ReadOnly)==false)
       QMessageBox::information(this, "shibai ", "失败！");
    QDataStream in(&file);
    QString str;
    in >> str;

    file.close();

    QFile f;
    f.setFileName("b.jpg");
    f.open(QIODevice::WriteOnly);
    QDataStream out(&f);
    out <<str;
    f.close();
}
